package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.DbanForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * 公告信息类
 * @author梁桂花
 *
 * 2018-10-27
 */
public class Dban {
	
	private static Log LOG =LogFactory.getLog(Dban.class);
	//定义属性
	private Integer id;
	private String	dnrong;
	private String	person;
	private String	posttime;
	
	private JdbcUtils jdbcUtils = new JdbcUtils();

	public Dban(Integer id){
		this.id =id;
	}
	
	
	/**
	 * 公告信息列表
	 * @return
	 */
	public List<Dban> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Dban> dbanList =new ArrayList<Dban>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造插入sql
			String sql="select * from t_dban where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String dnrong=rs.getString("dnrong");
				String person=rs.getString("person");
			    String	posttime=rs.getString("posttime");
			   
				
				 Dban model=new Dban(id,dnrong,person,posttime);
				
				dbanList.add(model);
							}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("加载待办事项失败!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return dbanList;
		
	}
	
     public void load()throws Exception{
 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null; 
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "select * from  t_dban where id='"+this.id+"'";
 		
 			rs=stmt.executeQuery(sql);
 			if(rs.next()){
 				this.id =rs.getInt("id");
 				this.dnrong =rs.getString("dnrong");
 				this.person =rs.getString("person");
 				this.posttime=rs.getString("posttime");
 			  	
 				
 			}
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 			throw new SQLException("加载待办事项信息");
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}	
 	}
 	public void editDban() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "update  t_dban  set dnrong='"+this.dnrong+"',person='"+this.person+"',posttime='"+this.posttime+"' where id='"+this.id+"'";
 			System.out.println("编辑待办事项信息");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void delDban() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			// 3:閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓绲奞L
 			String sql = "delete from t_dban  where id='"+this.id+"'";
 			// 4:鎵ч敓鏂ゆ嫹SQL閿熸枻鎷烽敓鏂ゆ嫹閿熸埅鏂ゆ嫹閿燂拷
 			System.out.println("删除待办事项");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void add(){
 		System.out.println("添加待办事项");
 		Connection conn =null;
 		Statement stmt=null;
 		try{
 			
 			 conn =jdbcUtils.getConn();
 		
 			String sql="insert into t_dban(dnrong,person,posttime) values('"+this.dnrong+"','"+this.person+"','"+this.posttime+"')";
 			
 			 stmt =conn.createStatement();
 			stmt.executeUpdate(sql);
 		}catch (Exception e) {
 			e.printStackTrace();
 			
 		}finally{
 			jdbcUtils.close(conn, stmt);
 			
 		}
 		
 	}
 	public Pager<Dban> list(int page,int size,DbanForm form) {

 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null;
 		Pager<Dban> results=new Pager<Dban>();
 		results.setPage(page);
 		results.setSize(size);
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			int start=(page-1)*size;
 			int limit=size;
 			//閿熸枻鎷疯SQL
 			String selectSql="select * from t_dban where 1=1";
 			if(form!=null&&form.getId()!=null&&!"".equals(form.getId())){
 				selectSql +=" and id like'%"+form.getId()+"%'";
 			}
 			
 			//缁熼敓鏂ゆ嫹SQL
 			String countSql ="select count(*)as c from ("+selectSql+") as t1";
 			//閿熸枻鎷烽〉SQL
 			 String pageSql=selectSql+" limit "+start+","+limit;
 			 rs= stmt.executeQuery(countSql);
 			 int total=0;
 			 while(rs.next()){
 				 total=rs.getInt("c");
 			 }
 			 results.setTotal(total);
 			 //閿熸枻鎷疯閿熸枻鎷烽〉閿熸枻鎷锋伅
 			 rs= stmt.executeQuery(pageSql);
 			 while(rs.next()){
 				 int id=rs.getInt("id");
 				String dnrong=rs.getString("dnrong");
				String person=rs.getString("person");
			    String	posttime=rs.getString("posttime");
			   
 				 Dban model=new Dban();
 				  model.setId(id);
 				  model.setDnrong(dnrong);
 				  model.setPerson(person);
 				  model.setPosttime(posttime);
 				 
 		
 				  results.getDataSets().add(model);
 			 }
 			 results.init();
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}
 		return results;

 	}
 	//定义无参构造函数
 		public Dban(){
 			
 		}

	public Dban( Integer id, String dnrong, String person,String posttime ) {
		super();
		this.id = id;
		this.dnrong = dnrong;
		this.person=person;
		this.posttime=posttime;
		
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getDnrong() {
		return dnrong;
	}


	public void setDnrong(String dnrong) {
		this.dnrong = dnrong;
	}


	public String getPerson() {
		return person;
	}


	public void setPerson(String person) {
		this.person = person;
	}


	public String getPosttime() {
		return posttime;
	}


	public void setPosttime(String posttime) {
		this.posttime = posttime;
	}


	public JdbcUtils getJdbcUtils() {
		return jdbcUtils;
	}


	public void setJdbcUtils(JdbcUtils jdbcUtils) {
		this.jdbcUtils = jdbcUtils;
	}


	
	

	
	
}
